use serde::{Deserialize, Serialize};

use crate::database::{self, table::user::model::NewUser};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserForm {
    pub id: i32,
    pub username: String,
    pub password: String,
    pub tid: i32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NewUserForm {
    pub username: String,
    pub password: String,
    pub tid: i32,
}

impl From<&database::table::user::model::User> for UserForm {
    fn from(user: &database::table::user::model::User) -> Self {
        UserForm {
            id: user.id,
            username: user.username.to_string(),
            password: user.password.to_string(),
            tid: user.tid,
        }
    }
}

impl UserForm {
    pub fn to(&self) -> NewUserForm {
        NewUserForm {
            username: self.username.to_string(),
            password: self.password.to_string(),
            tid: self.tid,
        }
    }
}

impl NewUserForm {
    pub fn to(&self) -> NewUser {
        NewUser {
            username: &self.username,
            password: &self.password,
            tid: self.tid,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoginForm {
    pub username: String,
    pub password: String,
}
